home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Learn C++ (CodeWarrior)
/
Chap 07.01 - time
/
time.cp
< prev
next >
Wrap
Text File
|
1995-10-20
|
1KB
|
103 lines
#include <iostream.h>
//--------------------------------------- Time
class Time
{
// Data members...
private:
short hours;
short minutes;
short seconds;
// Member functions...
void NormalizeTime();
public:
Time();
Time( short h, short m, short s );
void Display();
Time operator+( Time &aTime );
void operator*=( short num );
};
Time::Time()
{
seconds = 0;
minutes = 0;
hours = 0;
}
Time::Time( short h, short m, short s )
{
seconds = s;
minutes = m;
hours = h;
NormalizeTime();
}
void Time::NormalizeTime()
{
hours += ((minutes + (seconds/60)) / 60);
minutes = (minutes + (seconds/60)) % 60;
seconds %= 60;
}
void Time::Display()
{
cout << "(" << hours << ":" << minutes
<< ":" << seconds << ")\n";
}
Time Time::operator+( Time &aTime )
{
short h;
short m;
short s;
h = hours + aTime.hours;
m = minutes + aTime.minutes;
s = seconds + aTime.seconds;
Time tempTime( h, m, s );
return tempTime;
}
void Time::operator*=( short num )
{
hours *= num;
minutes *= num;
seconds *= num;
NormalizeTime();
}
//--------------------------------------- main
int main()
{
Time firstTime( 1, 10, 50 );
Time secondTime( 2, 24, 20 );
Time sumTime;
firstTime.Display();
secondTime.Display();
cout << "---------\n";
sumTime = firstTime + secondTime;
sumTime.Display();
cout << "* 2\n";
cout << "---------\n";
sumTime *= 2;
sumTime.Display();
return 0;
}